package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entity.CommonResult;
import com.atguigu.springcloud.entity.Payment;
import com.atguigu.springcloud.lb.MyLoadBalancer;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

@RestController
@RequestMapping("/consumer")
public class OrderController {
    private final RestTemplate restTemplate;
//    private static final String PAYMENT_URL = "http://localhost:8001";
    private static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    private final MyLoadBalancer myLoadBalancer;
    private final DiscoveryClient discoveryClient;

//    public OrderController(RestTemplate restTemplate) {
//        this.restTemplate = restTemplate;
//    }


    public OrderController(RestTemplate restTemplate, MyLoadBalancer myLoadBalancer, DiscoveryClient discoveryClient) {
        this.restTemplate = restTemplate;
        this.myLoadBalancer = myLoadBalancer;
        this.discoveryClient = discoveryClient;
    }

    @GetMapping("/payment/create")
    public CommonResult<Payment> create(Payment payment) {
//        return  restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
        ParameterizedTypeReference<CommonResult<Payment>> typeReference = new ParameterizedTypeReference<>() {};
        return restTemplate.exchange(PAYMENT_URL + "/payment/create", HttpMethod.POST, new HttpEntity<>(payment), typeReference).getBody();
    }

    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Integer id) {
//        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
        ParameterizedTypeReference<CommonResult<Payment>> typeReference = new ParameterizedTypeReference<>() {};
        return restTemplate.exchange(PAYMENT_URL + "/payment/get/" + id, HttpMethod.GET, null, typeReference).getBody();
    }

    @GetMapping("/payment/discovery")
    public Object discovery() {
        return restTemplate.getForObject(PAYMENT_URL + "/payment/discovery", Object.class);
    }

    @GetMapping("/payment/lb")
    public String getPaymentLB() {
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (instances == null || instances.size() <= 0) {
            return null;
        }
        ServiceInstance serviceInstance = myLoadBalancer.instances(instances);
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri + "/payment/lb", String.class);
    }
}
